package br.gov.cemaden.library.dao;

import br.gov.cemaden.library.model.Municipio;
import java.util.List;
import javax.persistence.EntityManager;
import javax.persistence.NoResultException;

/**
 *
 * @author alan
 */
public class MunicipioRepository {

    private EntityManager entityManager;
    
    public MunicipioRepository( EntityManager manager ){
        this.entityManager = manager;
    }

    public List<Municipio> list(){
        try{
            return entityManager.createQuery("from Municipio").setMaxResults(3).getResultList();
        }
        catch(NoResultException ex){
            return null;
        }
    }
    
    public Municipio getByGeoCodigo(Long geocodigo){
        try{
            return entityManager.createQuery("FROM Municipio WHERE codIBGE = :cod", Municipio.class)
                    .setParameter("cod", geocodigo.intValue()).getSingleResult();
        }
        catch(NoResultException ex){
            return null;
        }
    }

}
